//
//  UIImage+Extension.swift
//  Weibo2
//
//  Created by teacher on 16/11/19.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

extension UIImage {
    
    class func  snapShotCurrentWindow() -> UIImage {

        
        //截屏
        let window = UIApplication.shared.keyWindow!
        //开启图形上下文
        UIGraphicsBeginImageContextWithOptions(window.frame.size, false, 0)
        //将当前显示页面保存
         window.drawHierarchy(in: window.frame, afterScreenUpdates: true)
        // 从页面上获取图片
        let image = UIGraphicsGetImageFromCurrentImageContext()
        
        //关闭图形上文
        UIGraphicsEndImageContext()
        
        return image!
        
    }
//    
    //将图片缩放到指定宽度
    
    func scaleImage(widgth : CGFloat) -> UIImage{
        
        let imageW = self.size.width
        
        let imageH = self.size.height
        
        //判断大小
        if imageW < widgth{
            
            return self
        }
        let scaleH = imageH / imageW * widgth
        
        let imagebounds = CGRect(x: 0, y: 0, width: widgth, height: scaleH)
        
        //开启图形上下文
        
        UIGraphicsBeginImageContextWithOptions(imagebounds.size, false, 0)
        //绘制
        self.draw(in: imagebounds)
        //从上下文中取
        let image = UIGraphicsGetImageFromCurrentImageContext()
        //关闭上下文
        UIGraphicsEndImageContext()
        
        return image!
    }
    
    
    

    
    
    
}
